import socket
import sys

from utils.thread_util import createThread


class sockets_client:
    def __init__(self):
        try:
            self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.s.connect(('127.0.0.1', 6660))
        except socket.error as msg:
            print(msg)
            sys.exit(1)

    def recvmsg(self):
        self.thread = createThread(self.clientThread)
        self.thread.start()

    def clientThread(self):
        print(self.s.recv(1024))
        while 1:
            data = input('please input work: ').encode()
            self.s.send(data)
            print('服务器返回：', self.s.recv(1024).decode())
            if data == 'exit':
                break
        self.s.close()

if __name__ == '__main__':
    sockets_client().recvmsg()
